package boun.swe.bob.dao;

import boun.swe.bob.entities.UserMessage;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.util.List;

/**
 * This DAO corresponds to users messages
 */
@Component
public class UserMessageDao extends GenericDaoImpl<UserMessage, Integer>{

    private static final Logger LOG = Logger.getLogger(UserMessageDao.class);

    /**
     * This method is used for getting a message by specifying a message id
     *
     * @param messageId integer message id value
     * @return a result set containing a message
     */
        public UserMessage findByMessageId(final Integer messageId) {
        return (UserMessage) hibernateTemplate.execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                Query query = session.createSQLQuery("select * from user_message um where um.message_id= ?").addEntity("um", UserMessage.class);
                query.setInteger(0, messageId);
                return query.uniqueResult();
            }
        });
    }

    /**
     * This method is used for getting the inbox messages of a user.
     *
     * @param toUserId the integer user id value to get the inbox
     * @return the result set containing the inbox messages
     */
        public List<UserMessage> findInboxRootMessagesByUserId(final Integer toUserId) {
        return (List<UserMessage>) hibernateTemplate.execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                Query query = session.createSQLQuery("select * from user_inbox_view um where (um.to_user_id = ? or um.from_user_id= ?) and um.from_message_id is null order by um.message_id desc").addEntity("um", UserMessage.class);
                query.setInteger(0, toUserId);
                query.setInteger(1, toUserId);
                return query.list();
            }
        });
    }

    /**
     * This method is used for sending a message
     *
     * @param fromUserId an integer value of a user id that is sending a message
     * @param toUserId an integer value of a user id that is receiving a message
     * @param title a string value specifying the title of the message
     * @param messageBody a string containing the message body
     */
     public void addNewRootMessage(final int fromUserId,final int toUserId,final String title,final String messageBody) {
        hibernateTemplate.execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                Query query = session.createSQLQuery("insert into user_message(from_user_id,to_user_id,title,message_body,from_message_id) values(?,?,?,?,?)");
                query.setInteger(0,fromUserId);
                query.setInteger(1,toUserId);
                query.setString(2,title);
                query.setString(3,messageBody);
                query.setString(4,null);
                query.executeUpdate();
                return null;
            }
        });
    }

    /**
     * This method is used for sending a reply to a message thread
     *
     * @param fromMessageId an integer value of a message id that is being threaded
     * @param fromUserId an integer value of a user id that is sending a reply
     * @param toUserId an integer value of a user id that is receiving a message
     * @param title a string value specifying the title of the message
     * @param messageBody a string containing the message body
     */
         public void addNewSubMessage(final int fromMessageId,final int fromUserId,final int toUserId,final String title,final String messageBody) {
        hibernateTemplate.execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                Query query = session.createSQLQuery("insert into user_message(from_user_id,to_user_id,title,message_body,from_message_id) values(?,?,?,?,?)");
                query.setInteger(0,fromUserId);
                query.setInteger(1,toUserId);
                query.setString(2,title);
                query.setString(3,messageBody);
                query.setInteger(4,fromMessageId);
                query.executeUpdate();
                return null;
            }
        });
    }
    

    
}
